import { get, post, put, del } from '@/utils/request';

/**
 * 学习内容管理API服务
 */

// ==================== 检测文本管理 ====================

/**
 * 获取检测文本列表
 */
export async function getDetectionTextList(params: LEARNING.DetectionTextListParams): Promise<LEARNING.ApiResponse<{ list: LEARNING.DetectionTextModel[]; pagination: LEARNING.PaginationInfo }>> {
  return get('/admin/detection-texts', params);
}

/**
 * 获取检测文本详情
 */
export async function getDetectionTextDetail(id: number): Promise<LEARNING.ApiResponse<LEARNING.DetectionTextModel>> {
  return get(`/admin/detection-texts/${id}`);
}

/**
 * 创建检测文本
 */
export async function createDetectionText(data: LEARNING.CreateDetectionTextParams): Promise<LEARNING.ApiResponse<LEARNING.DetectionTextModel>> {
  return post('/admin/detection-texts', data);
}

/**
 * 更新检测文本
 */
export async function updateDetectionText(id: number, data: LEARNING.UpdateDetectionTextParams): Promise<LEARNING.ApiResponse<LEARNING.DetectionTextModel>> {
  return put(`/admin/detection-texts/${id}`, data);
}

/**
 * 删除检测文本
 */
export async function deleteDetectionText(id: number): Promise<LEARNING.ApiResponse<null>> {
  return del(`/admin/detection-texts/${id}`);
}

/**
 * 批量删除检测文本
 */
export async function batchDeleteDetectionTexts(ids: number[]): Promise<LEARNING.ApiResponse<null>> {
  return post('/admin/detection-texts/batch-delete', { text_ids: ids });
}

/**
 * 批量导入检测文本
 */
export async function importDetectionTexts(file: File): Promise<LEARNING.ApiResponse<{ success_count: number; error_count: number; errors: any[] }>> {
  const formData = new FormData();
  formData.append('file', file);

  return post('/admin/detection-texts/import', formData);
}

/**
 * 批量导入检测文本（别名）
 */
export async function batchImportTexts(file: File): Promise<LEARNING.ApiResponse<{ success_count: number; error_count: number; errors: any[] }>> {
  return importDetectionTexts(file);
}

/**
 * 下载导入模板
 */
export async function downloadImportTemplate(type: 'detection-text' | 'game-content' | 'daily-sentence'): Promise<Blob> {
  return get(`/admin/${type}/import-template`) as Promise<Blob>;
}

/**
 * 更新检测文本状态
 */
export async function updateDetectionTextStatus(id: number, status: 'active' | 'inactive'): Promise<LEARNING.ApiResponse<null>> {
  return put(`/admin/detection-texts/${id}/status`, { status });
}

/**
 * 导出检测文本
 */
export async function exportDetectionTexts(params: LEARNING.DetectionTextListParams & { format: 'excel' | 'csv' }): Promise<Blob> {
  return get('/admin/detection-texts/export', params) as Promise<Blob>;
}

/**
 * 生成拼音
 */
export async function generatePinyin(text: string): Promise<LEARNING.ApiResponse<{ pinyin: string }>> {
  return post('/admin/detection-texts/generate-pinyin', { text });
}

// ==================== 游戏内容管理 ====================

/**
 * 获取游戏内容列表
 */
export async function getGameContentList(params: LEARNING.GameContentListParams): Promise<LEARNING.ApiResponse<{ list: LEARNING.GameContentModel[]; pagination: LEARNING.PaginationInfo }>> {
  return get('/admin/game-contents', params);
}

/**
 * 获取游戏内容详情
 */
export async function getGameContentDetail(id: number): Promise<LEARNING.ApiResponse<LEARNING.GameContentModel>> {
  return get(`/admin/game-contents/${id}`);
}

/**
 * 创建游戏内容
 */
export async function createGameContent(data: LEARNING.CreateGameContentParams): Promise<LEARNING.ApiResponse<LEARNING.GameContentModel>> {
  return post('/admin/game-contents', data);
}

/**
 * 更新游戏内容
 */
export async function updateGameContent(id: number, data: LEARNING.UpdateGameContentParams): Promise<LEARNING.ApiResponse<LEARNING.GameContentModel>> {
  return put(`/admin/game-contents/${id}`, data);
}

/**
 * 删除游戏内容
 */
export async function deleteGameContent(id: number): Promise<LEARNING.ApiResponse<null>> {
  return del(`/admin/game-contents/${id}`);
}

/**
 * 批量删除游戏内容
 */
export async function batchDeleteGameContents(ids: number[]): Promise<LEARNING.ApiResponse<null>> {
  return post('/admin/game-contents/batch-delete', { content_ids: ids });
}

/**
 * 复制游戏内容
 */
export async function copyGameContent(id: number, title?: string): Promise<LEARNING.ApiResponse<LEARNING.GameContentModel>> {
  return post(`/admin/game-contents/${id}/copy`, { title });
}

/**
 * 更新游戏内容状态
 */
export async function updateGameContentStatus(id: number, status: 'active' | 'inactive'): Promise<LEARNING.ApiResponse<null>> {
  return put(`/admin/game-contents/${id}/status`, { status });
}

/**
 * 预览游戏内容
 */
export async function previewGameContent(id: number): Promise<LEARNING.ApiResponse<any>> {
  return get(`/admin/game-contents/${id}/preview`);
}

// ==================== 每日一句管理 ====================

/**
 * 获取每日一句列表
 */
export async function getDailySentenceList(params: LEARNING.DailySentenceListParams): Promise<LEARNING.ApiResponse<{ list: LEARNING.DailySentenceModel[]; pagination: LEARNING.PaginationInfo }>> {
  return get('/admin/daily-sentences', params);
}

/**
 * 获取每日一句详情
 */
export async function getDailySentenceDetail(id: number): Promise<LEARNING.ApiResponse<LEARNING.DailySentenceModel>> {
  return get(`/admin/daily-sentences/${id}`);
}

/**
 * 创建每日一句
 */
export async function createDailySentence(data: LEARNING.CreateDailySentenceParams): Promise<LEARNING.ApiResponse<LEARNING.DailySentenceModel>> {
  return post('/admin/daily-sentences', data);
}

/**
 * 更新每日一句
 */
export async function updateDailySentence(id: number, data: LEARNING.UpdateDailySentenceParams): Promise<LEARNING.ApiResponse<LEARNING.DailySentenceModel>> {
  return put(`/admin/daily-sentences/${id}`, data);
}

/**
 * 删除每日一句
 */
export async function deleteDailySentence(id: number): Promise<LEARNING.ApiResponse<null>> {
  return del(`/admin/daily-sentences/${id}`);
}

/**
 * 更新每日一句状态
 */
export async function updateDailySentenceStatus(id: number, status: 'draft' | 'scheduled' | 'published' | 'archived'): Promise<LEARNING.ApiResponse<null>> {
  return put(`/admin/daily-sentences/${id}/status`, { status });
}

/**
 * 安排每日一句
 */
export async function scheduleDailySentence(id: number, scheduled_date: string): Promise<LEARNING.ApiResponse<null>> {
  return put(`/admin/daily-sentences/${id}/schedule`, { scheduled_date });
}

/**
 * 批量删除每日一句
 */
export async function batchDeleteDailySentences(ids: number[]): Promise<LEARNING.ApiResponse<null>> {
  return post('/admin/daily-sentences/batch-delete', { sentence_ids: ids });
}

/**
 * 获取日期安排
 */
export async function getDailySentenceSchedule(year: number, month: number): Promise<LEARNING.ApiResponse<any>> {
  return get('/admin/daily-sentences/schedule', { year, month });
}

/**
 * 批量安排每日一句
 */
export async function batchScheduleDailySentences(data: Array<{ id: number; scheduled_date: string }>): Promise<LEARNING.ApiResponse<null>> {
  return post('/admin/daily-sentences/batch-schedule', { schedules: data });
}

/**
 * 自动安排每日一句
 */
export async function autoScheduleDailySentences(params: { start_date: string; end_date: string; category?: string }): Promise<LEARNING.ApiResponse<{ scheduled_count: number }>> {
  return post('/admin/daily-sentences/auto-schedule', params,
  );
}

// ==================== 学习内容统计 ====================

/**
 * 获取学习内容统计数据
 */
export async function getLearningStatistics(): Promise<LEARNING.ApiResponse<LEARNING.LearningStatistics>> {
  return get('/admin/statistics/learning-content');
}

// ==================== 通用功能 ====================

/**
 * 获取难度选项
 */
export async function getDifficultyOptions(): Promise<LEARNING.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/learning-content/difficulty-options');
}

/**
 * 获取分类选项
 */
export async function getCategoryOptions(type?: string): Promise<LEARNING.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/learning-content/category-options', { type });
}

/**
 * 获取游戏类型选项
 */
export async function getGameTypeOptions(): Promise<LEARNING.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/learning-content/game-type-options');
}

/**
 * 获取内容类型选项
 */
export async function getContentTypeOptions(): Promise<LEARNING.ApiResponse<Array<{ label: string; value: string }>>> {
  return get('/admin/learning-content/content-type-options');
}

/**
 * 验证游戏内容数据
 */
export async function validateGameContentData(data: any): Promise<LEARNING.ApiResponse<{ valid: boolean; errors?: string[] }>> {
  return post('/admin/learning-content/validate-game-data', data);
}
